package com.aiju.cash.ui.account;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.account.manager.AccountType;
import com.aiju.cash.ui.account.manager.AccountTypeManager;
import com.aiju.common.PixValue;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.T;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-30 10:22
 * @version: V1.0
 * @Description: 添加类目
 */
public class AccountTypeAddActivity extends BaseTitleActivity {

    private ImageView imageView;
    private EditText editText;
    private GridView gridView;

    private AccountType type = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_type_add);
        setCenterTitle("添加类目");

        setRightTitle("保存");

        final List<AccountType> accountTypes = AccountTypeManager.getAccountType();
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editText.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    T.ToastShort("请输入分类名称");
                    return;
                }
                if (imageView.getDrawable() == null) {
                    T.ToastShort("请选择分类图标");
                    return;
                }
                if (!CheckUtil.checkAccountName(name)){
                    T.ToastShort("分类名称仅支持1-4位汉字、数字和英文");
                    return;
                }
                AccountType addType = new AccountType(name, type.getPic(), accountTypes.size());
                accountTypes.add(addType);
                AccountTypeManager.saveAccountType(accountTypes);
                T.ToastShort("添加分类成功");
                finish();
            }
        });

        imageView = (ImageView) findViewById(R.id.iv_account_type_add);
        editText = (EditText) findViewById(R.id.ev_account_type_add);
        gridView = (GridView) findViewById(R.id.gv_account_type_add);

        gridView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return accountTypes.size();
            }

            @Override
            public Object getItem(int position) {
                return accountTypes.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView = new ImageView(AccountTypeAddActivity.this);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(PixValue.dp.valueOf(44), PixValue.dp.valueOf(44)));
                imageView.setImageResource(accountTypes.get(position).getPic());
                return imageView;
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                type = accountTypes.get(position);
                imageView.setVisibility(View.VISIBLE);
                imageView.setImageResource(type.getPic());
            }
        });
    }
}
